using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using csammisrun.shaim;

namespace Controls
{
  public partial class MetaContactPanel : StackPanel
  {
    #region Routed events
    public static readonly RoutedEvent MetaContactStatusClickEvent =
      EventManager.RegisterRoutedEvent("MetaContactStatusClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MetaContactPanel));
    /// <summary>
    /// Raised when a metacontact is selected in the UI
    /// </summary>
    public event RoutedEventHandler MetaContactStatusClick
    {
      add { AddHandler(MetaContactStatusClickEvent, value); }
      remove { RemoveHandler(MetaContactStatusClickEvent, value); }
    }

    public static readonly RoutedEvent MetaContactComingOnlineEvent =
      EventManager.RegisterRoutedEvent("MetaContactComingOnline", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MetaContactPanel));
    /// <summary>
    /// Raised when a metacontact changes from offline to available
    /// </summary>
    public event RoutedEventHandler MetaContactComingOnline
    {
      add { AddHandler(MetaContactComingOnlineEvent, value); }
      remove { RemoveHandler(MetaContactComingOnlineEvent, value); }
    }
    #endregion

    #region Dependency properties
    // Contact get/set, DisplayName, Status at a minimum, StatusSortingIndex maybe
    public static readonly DependencyProperty ContactProperty =
      DependencyProperty.Register("Contact", typeof(ShaimMetaContact), typeof(MetaContactPanel));
    /// <summary>
    /// Gets or sets the ShaimMetaContact represented by this panel
    /// </summary>
    public ShaimMetaContact Contact
    {
      get { return GetValue(ContactProperty) as ShaimMetaContact; }
      set
      {
        if (value != Contact)
        {
          value.DisplayNameUpdate += new ShaimMetaContact.DisplayNameUpdateHandler(value_DisplayNameUpdate);
          value.StatusUpdate += new ShaimMetaContact.StatusUpdateHandler(value_StatusUpdate);
          value_DisplayNameUpdate(value);
          value_StatusUpdate(value, ShaimContactStatus.Offline);

          SetValue(ContactProperty, value);
        }
      }
    }

    void value_StatusUpdate(ShaimMetaContact smc, ShaimContactStatus oldstatus)
    {
      
    }

    public static readonly DependencyProperty DisplayNameProperty =
      DependencyProperty.Register("DisplayName", typeof(string), typeof(MetaContactPanel));
    /// <summary>
    /// Gets the display name for this metacontact
    /// </summary>
    public string DisplayName
    {
      get { return GetValue(DisplayNameProperty) as string; }
    }
    void value_DisplayNameUpdate(ShaimMetaContact smc)
    {
      SetValue(DisplayNameProperty, smc.DisplayName);
    }

    #endregion

    private bool _fireclickevent = false;

    public MetaContactPanel()
    {
      InitializeComponent();
    }

    #region Metacontact status button click support
    private void MetaContactButton_MouseDown(object sender, MouseEventArgs e)
    {
      _fireclickevent = true;
    }

    private void MetaContactButton_MouseUp(object sender, MouseEventArgs e)
    {
      if (_fireclickevent)
      {
        RoutedEventArgs rea = new RoutedEventArgs(MetaContactStatusClickEvent, this);
        RaiseEvent(rea);
      }
      _fireclickevent = false;
    }

    private void MetaContactButton_MouseLeave(object sender, MouseEventArgs e)
    {
      _fireclickevent = false;
    }
    #endregion
  }
}